//
//  mmjTabBarController.m
//  Social
//
//  Created by seirra on 2018/5/12.
//  Copyright © 2018年 mmj. All rights reserved.
//

#import "mmjTabBarController.h"

@interface mmjTabBarController ()

@end

@implementation mmjTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /**** 设置所有UITabBarItem的文字属性 ****/
    [self setupItemTitleTextAttributes];
    
    /**** 添加子控制器 ****/
    [self setupChildViewControllers];
    
    /**** 更换TabBar ****/
    [self setupTabBar];
    

}


/**
 *  设置所有UITabBarItem的文字属性
 */
- (void)setupItemTitleTextAttributes
{
    UITabBarItem *item = [UITabBarItem appearance];
    // 普通状态下的文字属性
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:14];
    normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    // 选中状态下的文字属性
    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateSelected];
}

/**
 *  添加子控制器
 */
- (void)setupChildViewControllers
{
     [self setupchildviewcontroller:[[mmjNavigationController alloc]initWithRootViewController:[[mmjMeViewController alloc]init]] Title:@"我"Image:@"tabBar_me_icon" selectimage:@"tabBar_me_click_icon"];
    
    [self setupchildviewcontroller:[[mmjNavigationController alloc]initWithRootViewController:[[mmjEssenceViewController alloc]init]] Title:@"精华"Image:@"tabBar_essence_icon" selectimage:@"tabBar_essence_click_icon"];
    [self setupchildviewcontroller:[[mmjNavigationController alloc]initWithRootViewController:[[mmjNewsViewController alloc]init]] Title:@"新帖"Image:@"tabBar_new_icon" selectimage:@"tabBar_new_click_icon"];

    [self setupchildviewcontroller:[[mmjNavigationController alloc]initWithRootViewController:[[mmjFollowViewController alloc]init]] Title:@"关注"Image:@"tabBar_friendTrends_icon" selectimage:@"tabBar_friendTrends_click_icon"];
   
}

/**
 *  初始化一个子控制器
 *
 *  @param vc            子控制器
 *  @param title         标题
 *  @param image         图标
 *  @param selectedImage 选中的图标
 */
- (void)setupOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    vc.tabBarItem.title = title;
    if (image.length) { // 图片名有具体值
        vc.tabBarItem.image = [UIImage imageNamed:image];
        vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    }
    [self addChildViewController:vc];
}

/**
 *  更换TabBar
 */
- (void)setupTabBar
{
    [self setValue:[[mmjTabBar alloc] init] forKeyPath:@"tabBar"];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //只加载一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //增加一个发布按钮
        UIButton *publishbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        //publishbutton.backgroundColor = mmjradomcolor;
        [publishbutton setImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [publishbutton setImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        [publishbutton sizeToFit];
        publishbutton.frame = CGRectMake(0, 0, self.tabBar.frame.size.width/5, self.tabBar.frame.size.height);
        publishbutton.center = CGPointMake(self.tabBar.frame.size.width*0.5, self.tabBar.frame.size.height*0.5);
        [publishbutton addTarget:self action:@selector(publicClick) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:publishbutton];
    });
    
}
/*
 初始化一个字控制器
  */
-(void)setupchildviewcontroller:(UIViewController *)vc Title:(NSString *)Title Image:(NSString *)image selectimage:(NSString *)selectimage
{
    vc.tabBarItem.title = Title;
    if (image.length) {
        vc.tabBarItem.image = [UIImage imageNamed:image];
        vc.tabBarItem.selectedImage =[UIImage imageNamed:selectimage];
    }
    [self addChildViewController:vc];
}
//发布按钮点击事件
-(void)publicClick{
    mmjlogfunc;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
